﻿@model News
@{
    ViewBag.Title = "Edit";
    List<Category> categoryList = ViewBag.CategoryList ?? new List<Category>();
}
@section head{
    <style type="text/css">
        html {
            background: #fff;
        }
        .layui-anim-upbit {
            z-index: 9999 !important;
        }
    </style>
}
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    @Html.AntiForgeryToken()

    <input type="hidden" name="Id" value="@(Model?.Id??0)" />


    <div class="layui-form-item">
        @Html.LabelFor(model => model.Title, new { @class = "layui-form-label", @for = "Title" })
        <div class="layui-input-block">
            @Html.TextBoxFor(model => model.Title, new { lay_verify = "required", autocomplete = "off", @class = "layui-input" })
        </div>
    </div>

    <div class="layui-form-item">
        @Html.LabelFor(model => model.SubTitle, new { @class = "layui-form-label", @for = "SubTitle" })
        <div class="layui-input-block">
            @Html.TextBoxFor(model => model.SubTitle, new { lay_verify = "required", autocomplete = "off", @class = "layui-input" })
        </div>
    </div>

    <div class="layui-form-item">
        @Html.LabelFor(model => model.CategoryId, new { @class = "layui-form-label", @for = "CategoryId" })
        <div class="layui-input-block">
            <select class="layui-input" name="CategoryId" id="CategoryId">
                <option value="">请选择</option>
                @foreach (var item in categoryList)
                {
                    <option value="@item.Id" @Html.Raw(null != Model && Model.CategoryId == item.Id ? "selected=\"selected\"" : "")>@item.CategoryName</option>
                }
            </select>
        </div>
    </div>
    
    <div class="layui-form-item">
        @Html.LabelFor(model => model.Profile, new { @class = "layui-form-label", @for = "Profile" })
        <div class="layui-input-block">
            <script id="test_ue_container" name="Profile" type="text/plain">@Html.Raw(Model?.Profile)</script>
            @*@Html.TextBoxFor(model => model.Profile, new {lay_verify = "required", autocomplete = "off", @class = "layui-input"})*@
        </div>
    </div>

    @*<script id="test_ue_container" name="content" type="text/plain">
    </script>*@
    @*<div class="layui-form-item layui-layout-admin">
        <div class="layui-input-block">
            <div class="layui-footer" style="left: 0;">
                <button class="layui-btn" lay-submit="" lay-filter="layuiadmin-app-form-submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>*@
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>

@section scripts
{
    <script>
        layui.config({
            base: '/Content/LayuiAdmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
            }).use(['index', 'form', 'laydate','ueditor'], function () {
                var $ = layui.$, form = layui.form, admin = layui.admin, laydate = layui.laydate;
                laydate.render({ elem: '#CreateTime', type: 'datetime' });

                var ueditor = layui.ueditor;
            var ueditorHtml = '';
                var ueOption = { };//ue的option，可自定义，详见ue文档
            var UE = new ueditor.ui.Editor(ueOption);//固定写法，ueOption可不传
            UE.render("test_ue_container"); //容器id需要唯一。这种写法貌似可以在页面重新加载的时候重新渲染？
            UE.ready(function() {//初始化相关
                //设置编辑器的内容
                //UE.setContent('hello');
                //获取html内容，返回: <p>hello</p>
                ueditorHtml = UE.getContent();
                //获取纯文本内容，返回: hello
                var txt = UE.getContentTxt();
            });
									            //监听提交
            form.on('submit(layuiadmin-app-form-edit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                field.profile = UE.getContent();

                //提交 Ajax 成功后，关闭当前弹层并重载表格
                admin.req({
                    url: '@(Url.Action("Edit"))'
                    , type: "POST"
                    , data: field
                    , done: function (res) {
                        if (res.statusCode === 0) {
                            parent.layui.table.reload('LAY-app-content-list'); //重载表格
                            parent.layer.close(index); //再执行关闭
                            parent.parent.layer.msg(res.message, { offset: '15px', icon: 1 });
                        } else {
                            parent.parent.layer.msg(res.message, { offset: '15px', icon: 5 });
                        }
                    },
                    error: function (res) {
                        console.log('error='+res);
                    }
                });
            });
        })
    </script>
}

